package com.xjnt.base.support.admin.common.beetl.tags;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import org.beetl.core.GeneralVarTagBinding;

import com.google.common.collect.Lists;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Record;
import com.xjnt.base.support.admin.entity.Function;
import com.xjnt.base.support.admin.entity.Permission;
import com.xjnt.base.support.admin.service.FunctionService;
import com.xjnt.base.support.admin.service.PermissionService;
import com.xjnt.frameworks.plugin.shiro.ShiroMethod;

public class ToolbarTag extends GeneralVarTagBinding{

	private static final PermissionService permService = new PermissionService();
	private static final FunctionService funcService = new FunctionService();
	
	private String menuId;
	private List<String> elemIds = Lists.newArrayList();
	
	private void init(){
		menuId = getAttributeValue("menuId").toString();
		String strIds = getAttributeValue("elemIds").toString();
		if(StrKit.notBlank(strIds)){
			String[] ids = strIds.split(",");
			elemIds = Arrays.asList(ids);
		}
	}
	/*
	 * <button id="addFunction" class="btn btn-info btn-sm no-border" 
            onclick="goAdd('新增功能', 'system/admin/function/add?type=0', 'table-function');">
            <i class="ace-icon fa fa-plus bigger-110"></i>新增
       </button>
	 */
	@Override
	public void render() {
		init();
		// 获取菜单下的所有按钮权限
		List<Record> lstFuncPerms = permService.findFunctionPermissionByMenuId(menuId);
		// 检测当前用户是否拥有权限，并存储权限标识
		List<String> lstPermIds = Lists.newArrayList();
		for (Record funcPerm : lstFuncPerms) {
			if(funcPerm.getInt(Permission.AVAILABLE) == 1){
				String permission = funcPerm.getStr(Permission.CODE);
				boolean hasPerm = ShiroMethod.hasPermission(permission);
				if(hasPerm){
					lstPermIds.add(funcPerm.getStr("permid"));
				}
			}
		}
		// 根据权限ID获取所有按钮
		List<Function> lstFunctions = Lists.newArrayList();
		if(!lstPermIds.isEmpty()){
			lstFunctions = funcService.findByPermIds(lstPermIds.toArray());
		} 

		StringBuilder html = new StringBuilder();
		for (Function function : lstFunctions) {
			if(elemIds.isEmpty()){
				html.append("<button id=\"").append(function.getStr(Function.ELEM_ID)).append("\" ");
				html.append("class=\"").append(function.getStr(Function.ELEM_STYLE)).append("\" ");
				html.append(function.getStr(Function.ACT_EVT)).append("=\"").append(function.getStr(Function.ACT_URL)).append("\">\n");
				html.append("<i class=\"").append(function.getStr(Function.ELEM_ICON)).append("\"></i>");
				html.append(function.getStr(Function.NAME)).append("\n</button>");
			} else {
				if(elemIds.contains(function.getStr(Function.ELEM_ID))){
					html.append("<button id=\"").append(function.getStr(Function.ELEM_ID)).append("\" ");
					html.append("class=\"").append(function.getStr(Function.ELEM_STYLE)).append("\" ");
					html.append(function.getStr(Function.ACT_EVT)).append("=\"").append(function.getStr(Function.ACT_URL)).append("\">\n");
					html.append("<i class=\"").append(function.getStr(Function.ELEM_ICON)).append("\"></i>");
					html.append(function.getStr(Function.NAME)).append("\n</button>");
				}
			}
		}
		
		try {
			ctx.byteWriter.writeString(html.toString());
			this.doBodyRender();
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

}
